/**
 * Created by LMN on 2016/9/18.
 */
var auth = require('./auth.js').obj;
var user_auth = require('../config').user_auth;
var io = require('./io.js');
var callback = {};


module.exports = {
    /**
     * Save the "send" function of a worker.
     * @param flag , the unique flag for the worker.
     * @param cb , THE function.
     */
    sendFunToWorker: function(flag,cb){
        callback[flag] = cb;
    },
    /**
     * Send object to  the "send" function of a worker.
     * @param obj , the object.
     */
    sendToSocketIO: function(obj){
        //Sent to all the users who have the auth.
        var code = obj.code;
        if(code){
            for(var name in auth){
                if(auth[name]==user_auth.admin) io.send(name,code,obj);
                else if(auth[name]==user_auth.operator && name==code) io.send(name,code,obj);
            }
        }
    },
    /**
     * Send object to a worker by calling the "send" function which has been saved.
     * @param flag , the unique flag for the worker.
     * @param data , THE object.
     */
    sendToWorker: function (flag,data) {
        callback[flag](data);
    }
};